<?php

class ListControl extends CommonControl {

        public function index() {
                $this->assign_head();
                $this->_cate_info();
                $where = isset($_GET['where']) ? (int) $_GET['where'] : 0;
                $cid = (int) $_GET['cid'];
                $Temp = M('category')->query('SELECT * FROM hd_category');
                $allCid = $this->sonCate($Temp, $cid);
                $cids = '';
                foreach ($allCid as $v) {
                        $cids.=$v['cid'] . ',';
                }
                $cids = rtrim($cids, ',');
                $connect = ' and c.cid in(' . $cids . ')';
                if ($cid == 0) {
                        $connect = '';
                }

                switch ($where) {
                        case 1: $sql = 'SELECT * FROM hd_ask as a join hd_category as c on a.cid=c.cid where solve=1 ' . $connect;
                                break;
                        case 2: $sql = 'SELECT * FROM hd_ask as a join hd_category as c on a.cid=c.cid where reward>=20 ' . $connect;
                                break;
                        case 3: $sql = 'SELECT * FROM hd_ask as a join hd_category as c on a.cid=c.cid where answer=0 ' . $connect;
                                break;
                        default: $sql = 'SELECT * FROM hd_ask as a join hd_category as c on a.cid=c.cid where solve=0 ' . $connect;
                                break;
                }
                $question = M('category')->query($sql);
                $this->assign('question', $question);

                $this->display();
        }

        private function sonCate($data, $cid) {
                $arr = array();
                foreach ($data as $v) {
                        if ($v['cid'] == $cid) {
                                $arr[] = $v;
                                foreach ($data as $value) {
                                        if ($value['pid'] == $v['cid']) {
                                                $arr = array_merge($arr, $this->sonCate($data, $value['cid']));
                                        }
                                }
                        }
                }
                return $arr;
        }

}
